//
//  GRMyEvaluateViewController.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/26.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRMyEvaluateViewController.h"
#import "GROrderCommentTableViewCell.h"
#import "RequestManager+GROrderManager.h"
#import "GRCommentModel.h"
#import "GRBlankSpace.h"
#import "GRPhotoBrowserVC.h"
#import "GRTransitionAnimator.h"
@interface GRMyEvaluateViewController ()<UITableViewDelegate,UITableViewDataSource,GROrderCommentTableViewCellDelegate,UINavigationControllerDelegate>
{
    NSInteger currentIndex;
}
@property(nonatomic,strong)NSMutableArray * commentArr;
@property(nonatomic,strong)GRBlankSpace *blankSpceView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topOffset;

@end

@implementation GRMyEvaluateViewController
-(GRBlankSpace *)blankSpceView{
    if (!_blankSpceView) {
        _blankSpceView = [GRBlankSpace xx_instantiateFromNib];
        _blankSpceView.contentMode =  GRBlankContentModeEmptyEvaluation;
        _blankSpceView.frame = CGRectMake(0, 0, self.commentList.bounds.size.width, self.commentList.bounds.size.height);
        [self.commentList addSubview:_blankSpceView];
    }
    return _blankSpceView;
}
-(NSMutableArray *)commentArr{
    if (!_commentArr) {
        _commentArr = [NSMutableArray array];
    }
    return _commentArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
      self.topOffset.constant = IS_IPHONEX?88:64;
    [self.commentList registerNib:[UINib nibWithNibName:@"GROrderCommentTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"commentCell"];
    WEAKSELF
    self.commentList.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        currentIndex = 1;
        [weakSelf loadData:GRRefreshForHeader];
    }];
    self.commentList.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        currentIndex ++;
        [weakSelf loadData:GRRefreshForFooter];
    }];
    [self.commentList.mj_header beginRefreshing];
    
   
}
-(void)loadData:(GRRefresh)freshType{
    NSDictionary * dic = @{
                           @"pageIndex":@(currentIndex),
                           @"pageSize":@(10)
                           };
    [RequestManager getMyEvaluate:dic CompleteHander:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            NSArray * commentListArr = responseObject[@"evaluateList"];
            if (freshType == GRRefreshForHeader) {
                [self.commentArr removeAllObjects];
            }
            if(Array(commentListArr)){
                for (NSDictionary * commentDic in commentListArr) {
                    GRCommentModel * commentModel = [GRCommentModel yy_modelWithDictionary:commentDic];
                    [self.commentArr addObject:commentModel];
                }
            }
            self.blankSpceView.hidden = self.commentArr.count >0?YES:NO;
            [self.commentList reloadData];
            [self.commentList.mj_footer endRefreshing];
            [self.commentList.mj_header endRefreshing];
        }
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GROrderCommentTableViewCell * commentCell = [tableView dequeueReusableCellWithIdentifier:@"commentCell" forIndexPath:indexPath];
    commentCell.commentModel = self.commentArr[indexPath.row];
    commentCell.delegate = self;

  
    return commentCell;
    
}
-(void)seleteImageIndex:(NSInteger)index imagArr:(NSArray *)imageArr{
    
    GRPhotoBrowserVC * photoBrowser = [[GRPhotoBrowserVC alloc]init];
    photoBrowser.currentImageIndex = index;
    photoBrowser.imageArr = imageArr;
    self.navigationController.delegate = self;
    [self.navigationController pushViewController:photoBrowser animated:YES];
}

#pragma UINavigationDeleagte
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
    id <UIViewControllerAnimatedTransitioning> animationController;
    if ([toVC isKindOfClass:[GRPhotoBrowserVC class]]&& operation == UINavigationControllerOperationPush) {
        GRTransitionAnimator*  GRPushAnimation = [[GRTransitionAnimator alloc]init];
        GRPushAnimation.duration = 0.25;
        
        animationController =  GRPushAnimation;
        
    }else if([fromVC isKindOfClass:[GRPhotoBrowserVC class]] && operation == UINavigationControllerOperationPop ){
        GRTransitionAnimator*  GRPopAnimation = [[GRTransitionAnimator alloc]init];
        GRPopAnimation.duration = 0.25;
        animationController =  GRPopAnimation;
    }
    return animationController;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    GRCommentModel * commentModel = self.commentArr[indexPath.row];
    
    return [commentModel myEvaluateHeight];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.commentArr.count;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
